package com.shopoo.oauth.infrastructure.repository;

import javax.annotation.Resource;

import com.google.gson.Gson;
import com.shopoo.oauth.domain.repository.MsgRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Repository;

/**
 * @Description:
 * @Auther: limy66
 * @Date: 2021/04/25/15:44
 */
@Slf4j
@Repository
public class MsgRepositoryImpl implements MsgRepository {

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Value("${rocketmq.topic.oauth2.permission}")
    private String permissionTopic;

    @Override
    public void sendUpdatePermission() {
        log.info("发送更新权限通知");
        SendResult sendResult = rocketMQTemplate.syncSend(permissionTopic, MessageBuilder.withPayload("updateNotify").build());
        if (!SendStatus.SEND_OK.equals(sendResult.getSendStatus())) {
            log.error("发送更新权限通知失败：{}", new Gson().toJson(sendResult));
        }
    }
}
